<?php
//error_reporting(0);
session_start();
//初始化
define('INC_P',dirname(__FILE__));
include_once(INC_P.'/logger.php');
include_once(INC_P.'/config.php');
include_once(INC_P.'/db.php');
include_once(INC_P.'/var.php');
include_once(INC_P.'/fun.php');
require_once getCacheFilePath('template.php');
require_once getCacheFilePath('groups.php');
require_once getCacheFilePath('settings.php');
require_once getCacheFilePath('credits.php');
if($cache_settings['isgzip']=='1' && extension_loaded('zlib')){
	ob_start('ob_gzhandler');
	header("Content-Type: text/html; charset=UTF-8");
	header("Content-Encoding: gzip");
	header("Vary: Accept-Encoding");
}
ob_start("_clear_");

//是否关闭论坛
if($cache_settings['isoff']=='1' && !stristr($_SERVER['PHP_SELF'],'login.php') && !stristr($_SERVER['PHP_SELF'],'/admin/')){
	exit('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'.$cache_settings['bbsname'].'</title></head><body>'.$cache_settings['offdetails'].'</body></html>');
}

if(stristr($_SERVER['SERVER_NAME'],"6kbbs.com")){
	_header_("HTTP/1.1 301 Moved Permanently");
	_header_("location:http://www.6kbbs.net{$_SERVER['REQUEST_URI']}");
}

$timer_begin=getmicrotime();
$db = new db();
$db->connect($_DB);


if (!get_magic_quotes_gpc()){if(!empty($_GET)){$_GET  = addslashes_deep($_GET);}	if (!empty($_POST)){$_POST = addslashes_deep($_POST);}	$_COOKIE   = addslashes_deep($_COOKIE);		$_REQUEST  = addslashes_deep($_REQUEST);}
unregister_globals('_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');

//模板路径

$_SYS['styleid'] = getCookies('styleid');
if(empty($_SYS['styleid']) || empty($cache_template[$_SYS['styleid']])){
	foreach($cache_template as $tmptpl){
		$_SYS['styleid']=$tmptpl['default'];
		setCookies("styleid",$_SYS['styleid'],3600*24*365);
		break;
	}
}

//会员登录信息

$lg['userid'] = numFilter($_SESSION['userid']);
$lg['groupid'] = numFilter($_SESSION['groupid']);
$lg['isadmin'] = numFilter($_SESSION['isadmin']);
$lg['username'] = strFilter(getCookies('username'));
$lg['displayname'] = htmlFilter(getCookies('username'));
$lg['userpass'] = strFilter(getCookies('userpass'));
$lg['userauth'] = strFilter(getCookies('userauth'));
$lg['userhidden'] = numFilter(getCookies('userhidden'));
$lg['expire'] = numFilter(getCookies('expire'));
if(empty($lg['username']) || empty($lg['userpass'])){
	$lg['userid']=0;$lg['groupid']=0;
}elseif(md5($lg['username'].$lg['userpass'].$cache_settings['salt'])!=$lg['userauth']){
	$lg['userid']=0;$lg['groupid']=0;$lg['username']='';$lg['userpass']='';
}
if($lg['userid']==0 || $lg['groupid']==0){
	$lg['groupid']=GROUP_GUESS;
	//自动登陆
	if(!empty($lg['username']) && !empty($lg['userpass'])){
		$lgrow=$db->row_select_one("users","username='{$lg[username]}' and userpass='{$lg[userpass]}'","id,groupid,postsnum,credits1,credits2,credits3,credits4,credits5,credits6,credits7,credits8");
		if(empty($lgrow)){
			$lg['groupid']=GROUP_GUESS;
		}else{
			if($lgrow['groupid']==GROUP_NOVERIFY || $lgrow['groupid']==GROUP_NOVISIT || $lgrow['groupid']==GROUP_VERIFYFAILED){	//待验证 //禁止访问 //验证不通过
				$cleart= -86400 * 365 * 2; setCookies('username', '', $cleart); setCookies('userpass', '', $cleart); setCookies('expire', '', $cleart); _header_("location:index.php");
			}else{
				if(numFilter($lgrow['groupid'])<=80){
					$allmark=getAllCredits($cache_settings['creditsexpression'], $lgrow);
					$lgrow_group=$db->row_select_one("groups","({$allmark} BETWEEN marklower AND markhigher) and (groupid BETWEEN 1 AND 80)","groupid");
					if(empty($lgrow_group)){
						printMsg('login_nogroup');
					}else{
						$lgrow['groupid']=$lgrow_group['groupid'];
					}
				}
				setCookies('userauth', md5($lg['username'].$lg['userpass'].$cache_settings['salt']), $lg['expire']);
				$_SESSION['userid'] = $lgrow['id'];
				$_SESSION['groupid'] = $lgrow['groupid'];
				$lg['userid'] = numFilter($_SESSION['userid']);
				$lg['groupid'] = numFilter($_SESSION['groupid']);
			}
		}
	}
}

$_SYS['TP'] = 'template/'.$_SYS['styleid'];
$_SYS['indexurl'] = genUrl('index.php'); 
$_SYS['positionindex'] = "<a href=\"{$_SYS['indexurl']}\">{$cache_settings['bbsname']}</a>";
$_SYS['userurl'] = genUrl("user.php?uid={$lg['userid']}");
?>